/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			ETSI_SText.cc
  \date			september 2011
  \author		TNick

   \brief		Contains the implementation for ETSI_SText class


   Copyright (c) 2011, Nicu Tofan
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:
   * Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   * Neither the name of the copyright holder nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND
   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   DISCLAIMED. IN NO EVENT SHALL NICU TOFAN BE LIABLE FOR ANY
   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

   */
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<QFontMetrics>
#include	<QPainter>

#include	<sheet/ETI_CplxTxt.h>
#include	<sheet/ETSheetView.h>
#include	<sheet/ETSheet.h>

#include	"ETSI_SText.h"

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */
DDHC_CONTENT(ETSI_SText)

/* ------------------------------------------------------------------------- */
ETSI_SText::ETSI_SText	( void )
	: ETSubItem()
{
	s_text = QObject::tr(STEXT_DEFAULT, "ETSI_SText default text");

	DDHC_CONSTRUCT;
	DDHC_SUBITEM_VARS;
	DDHC_VARIABLE(s_text);

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ETSI_SText::~ETSI_SText	( void )
{
	DDHC_DESTRUCT
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			ETSI_SText::setText			(QString new_text, bool b_recomp)
{
	ETI_CplxTxt * top_it;

	/* store new text */
	if (new_text.isEmpty())
		{
			s_text = QObject::tr(STEXT_DEFAULT, "ETSI_SText default text");
			setUninitialised();
		}
	else
		{
			s_text = new_text;
			setInitialised();
		}

	/* mark the item "dirty" */
	setSizeDirty(&top_it);

	/* recompute the item */
	if (b_recomp == true)
		{
			top_it->recompute();
		}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			ETSI_SText::recompute		(bool b_full)
{
	Q_UNUSED(b_full);

	/* reset the flags that request a recompute */
	resetSizeFlags();

	/* and compute the bounding box */
	QFontMetrics fi(font());
	qreal h = fi.height();
	bbox = QRectF(0, -(h / 2), fi.width(s_text), h);
	// QRectF(qreal left, qreal top, qreal width, qreal height);

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            ETSI_SText::paint               (
	ETSheetView * vp, QPainter * painter, QPointF pt_ref)
{

	PaintData	pd(vp, painter, pt_ref);
	prePaint(&pd);

	painter->drawText(
				pd.t_box,
				s_text);

	postPaint(&pd);

}
/* ========================================================================= */

///* ------------------------------------------------------------------------- */
//void			ETSI_SText::navKey		(
//	ETSheetView * vp, Qt::Key	key)
//{
//	/* it is implied that this is the current item in the viewport */
//	Q_ASSERT(vp->currentSubI() == this);

//	switch (key)		{
//	case	Qt::Key_Up:			{
//			navLeaveTop(vp->currentItem(), vp);
//			break;}
//	case	Qt::Key_Down:		{
//			navLeaveBtm(vp->currentItem(), vp);
//			break;}
//	case	Qt::Key_Left:		{
//			if (vp->selIsLeft())
//				{
//					navLeaveLeft(vp->currentItem(), vp);
//				}
//			else
//				{
//					vp->setSelLeft();
//				}
//			break;}
//	case	Qt::Key_Right:		{
//			if (vp->selIsLeft())
//				{
//					vp->setSelRight();
//				}
//			else
//				{
//					navLeaveRight(vp->currentItem(), vp);
//				}
//			break;}
//	default:					{
//		qDebug() << "ETSI_SText::navKey finds the value "
//				 << key << " inappropriate.";
//		break;}
//	}

//	/* this may not be the best approach, as it tiggersa redraw in all
//	  viewports and it redraws entire item while it might be that only a
//	  part must be redrawn */
//	vp->currentItem()->update();
//}
///* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			ETSI_SText::hitTest		(HtData * h_data)
{

	h_data->it_hitted = this;
	h_data->true_hit = true;
	/* h_data->rct_bbx already set by the caller */
	h_data->int_data = 0;

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ETSI_SText *	ETSI_SText::loadXML		(
	QDomElement dom_e)
{

	QDomElement dom_it;
	ETSI_SText * ret_it;

#ifdef	APP_DEBUG
	/* type is assumed to be root */

	dom_it = dom_e.firstChildElement(XML_TAG_TYPE);
	if (!dom_it.isNull())
		{
			Q_ASSERT_X(dom_it.text() == XML_SIT_STEXT, __FUNCTION__,
					   QString("ETSI_Root::loadXML got an element of type <"
					   "%1> while expected type is " XML_SIT_STEXT)
					   .arg(dom_it.text()).toLatin1()
					   );
		}
	else
		{
			qDebug() << "ETSI_SText::loadXML got DOM element without type";
		}
#endif

	/* create this element */
	ret_it = new ETSI_SText();

	/* the one and only common data to all kinds of sub-items */
	dom_it = dom_e.firstChildElement(XML_TAG_COMM);
	if (!dom_it.isNull())
		{
			ETSubItem::loadXMLCmn(ret_it, dom_it);

			if (!dom_it.nextSiblingElement(XML_TAG_COMM).isNull())
				{
					qDebug() << "The input presented more than one "
							 << "common tags, when only one per sub-item element "
							 << "is allowed";
				}
		}

	/* load particular data */

	dom_it = dom_e.firstChildElement(XML_TAG_TEXT);
	if (!dom_it.isNull())
		{
			ret_it->setText(dom_it.text(), false);
			if (!dom_it.nextSiblingElement(XML_TAG_TEXT).isNull())
				{
					qDebug() << "The input presented to ETSI_SText::loadXML more than one "
							 << "text tags, when only one per sub-item element "
							 << "is allowed";
				}
		}

	return ret_it;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void		ETSI_SText::saveXML		(
	QDomElement dom_e)
{
	/* save the type */
	QDomElement dom_it = dom_e.ownerDocument().createElement(XML_TAG_TYPE);
	dom_e.appendChild(dom_it);
	QDomText dom_tx = dom_e.ownerDocument().createTextNode(XML_SIT_STEXT);
	dom_it.appendChild(dom_tx);


	/* save common properties */
	dom_it = dom_e.ownerDocument().createElement(XML_TAG_COMM);
	dom_e.appendChild(dom_it);
	saveXMLCmn(dom_it);


	/* now specifics */
	dom_it = dom_e.ownerDocument().createElement(XML_TAG_TEXT);
	dom_e.appendChild(dom_it);
	dom_tx = dom_e.ownerDocument().createTextNode(text());
	dom_it.appendChild(dom_tx);

}
/* ========================================================================= */


/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
